package c.test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;

public class FileMode {
    /** Event type: Data was read from a file */
    public static final int ACCESS = 0x00000001;
    /** Event type: Data was written to a file */
    public static final int MODIFY = 0x00000002;
    /**
     * Event type: Metadata (permissions, owner, timestamp) was changed
     * explicitly
     */
    public static final int ATTRIB = 0x00000004;
    /**
     * Event type: Someone had a file or directory open for writing, and closed
     * it
     */
    public static final int CLOSE_WRITE = 0x00000008;
    /**
     * Event type: Someone had a file or directory open read-only, and closed it
     */
    public static final int CLOSE_NOWRITE = 0x00000010;
    /** Event type: A file or directory was opened */
    public static final int OPEN = 0x00000020;
    /**
     * Event type: A file or subdirectory was moved from the monitored directory
     */
    public static final int MOVED_FROM = 0x00000040;
    /**
     * Event type: A file or subdirectory was moved to the monitored directory
     */
    public static final int MOVED_TO = 0x00000080;
    /**
     * Event type: A new file or subdirectory was created under the monitored
     * directory
     */
    public static final int CREATE = 0x00000100;
    /** Event type: A file was deleted from the monitored directory */
    public static final int DELETE = 0x00000200;
    /**
     * Event type: The monitored file or directory was deleted; monitoring
     * effectively stops
     */
    public static final int DELETE_SELF = 0x00000400;
    /**
     * Event type: The monitored file or directory was moved; monitoring
     * continues
     */
    public static final int MOVE_SELF = 0x00000800;

    /** Event mask: All valid event types, combined */
    public static final int ALL_EVENTS = ACCESS | MODIFY | ATTRIB | CLOSE_WRITE | CLOSE_NOWRITE | OPEN | MOVED_FROM
            | MOVED_TO | DELETE | CREATE | DELETE_SELF | MOVE_SELF;

    public static void main(String[] args) {
        // print();
        
        String fileName = "ad==6.1.0.001&&ad1.0_1491718919164_envelope.log";
        System.out.println(fileName);
        System.out.println(fileName.replaceFirst("ad", "a"));
        
//        fileName="a"+fileName.substring(2, fileName.length());
//        System.out.println(fileName);
//        
//        String name = fileName.split("_")[0];
//        System.out.println("处理前 " + name);
//        name = name.replaceAll("==", "/")
//                .replaceAll("&&", " ")
//                .replaceAll("&=", " ")
//        ;
//        System.out.println("加工过文件名字 " + name);
    }

    private static void oom() {
        Throwable ex = new Throwable();
        System.out.println(getStackTrace(ex));
//        StackTraceElement[] stackElements = ex.getStackTrace();
//        if (stackElements != null) {
//            for (int i = 0; i < stackElements.length; i++) {
//                System.out.print(stackElements[i].getClassName() + "/t");
//                System.out.print(stackElements[i].getFileName() + "/t");
//                System.out.print(stackElements[i].getLineNumber() + "/t");
//                System.out.println(stackElements[i].getMethodName());
//                System.out.println("-----------------------------------");
//            }
//        }

        
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try{
            ex.printStackTrace(new PrintStream(baos));
        }finally{
            try {
                baos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println(baos.toString());
    }

    public static String getStackTrace(Throwable e) {
        StringWriter sw = null;
        PrintWriter pw = null;
        try {
            sw = new StringWriter();
            pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            pw.flush();
            sw.flush();
        } finally {
            if (sw != null) {
                try {
                    sw.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (pw != null) {
                pw.close();
            }
        }
        return sw.toString();
    }

    private static void print() {
        System.out.println("ACCESS:" + ACCESS);
        System.out.println("MODIFY:" + MODIFY);
        System.out.println("ATTRIB:" + ATTRIB);
        System.out.println("CLOSE_WRITE:" + CLOSE_WRITE);
        System.out.println("CLOSE_NOWRITE:" + CLOSE_NOWRITE);
        System.out.println("OPEN:" + OPEN);
        System.out.println("MOVED_FROM:" + MOVED_FROM);
        System.out.println("MOVED_TO:" + MOVED_TO);
        System.out.println("CREATE:" + CREATE);
        System.out.println("DELETE:" + DELETE);
        System.out.println("DELETE_SELF:" + DELETE_SELF);
        System.out.println("MOVE_SELF:" + MOVE_SELF);
        System.out.println("ALL_EVENTS:" + ALL_EVENTS);
    }

}
